/**
 * 
 */
package ca.utoronto.cms.cscc85.project4.teamrf;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
/**
 * This activity allows the user to see all recipes
 *
 */
public class ViewResultsActivity extends ListActivity {
	ListView resultsList; //creates the list object
	
	String[] columns = { "name", "time", "_id"}; //the information from each recipe hte user will see
	
	/**
	 *  Called when the activity is first created.
	 *  
	 *
	 */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewresults);
        
        resultsList = this.getListView(); //sets up the list object
        resultsList.setAdapter(new ArrayAdapter<String>(this, R.layout.resultentry, columns));
        resultsList.setTextFilterEnabled(true);

        //takes the sql query results and put them into the list view object
        startManagingCursor(Main.results);
        
        int[] to = new int[] { R.id.nameText, R.id.prepText, R.id.idLabel };
        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.resultentry, Main.results, columns, to);
        this.setListAdapter(cursorAdapter);

        //when an list object is clicked, go to view that object with the view recipe activity 
        resultsList.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Main.results.moveToPosition(position);
             
    			
    			// create intent and pass bundle
    			Intent viewIntent = new Intent(getApplicationContext(), ViewRecipeActivity.class);
    			Bundle bundle = new Bundle();
    			bundle.putString("name", Main.results.getString(1));
    			bundle.putString("ingredients", Main.results.getString(2));
    			bundle.putString("directions", Main.results.getString(3));
    			bundle.putInt("time", Main.results.getInt(4));
    			viewIntent.putExtras(bundle);
    			startActivity(viewIntent);
    			

            }
        });
        
        /*
        list = (ListView)findViewById(R.id.cat_listing);
        list.setAdapter(new ArrayAdapter<String>(this,
            R.layout.categories, categories));
        list.setTextFilterEnabled(true);

        Cursor cursor = data.rawQuery("SELECT term, desc FROM words WHERE cat = '" + categories[position] + "'", null);
        startManagingCursor(cursor);

        String columns[] = new String[] { "term", "desc" };
        int[] to = new int[] { R.id.cat_term, R.id.cat_desc };

        SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.cat_result, cursor, columns, to);
        this.setListAdapter(myAdapter);
*/
        //listView.setAdapter(adapter);
/*        LinearLayout entry = (LinearLayout) findViewById(R.id.resultEntry);
        ((TextView) entry.getChildAt(0)).setText("Title 1");
        ((TextView) entry.getChildAt(1)).setText("SubTitle 1");
        resultsList.addView(entry);
        
        ((TextView) entry.getChildAt(0)).setText("Title 2");
        ((TextView) entry.getChildAt(1)).setText("SubTitle 2");
        resultsList.addView(entry);

        resultsList.setTextFilterEnabled(true);
*//*
        @Override
        public void onCreate(Bundle icicle){
            super.onCreate(icicle);
            setContentView(R.layout.cat_list);

            list = (ListView)findViewById(R.id.cat_listing);
            list.setAdapter(new ArrayAdapter<String>(this,
                R.layout.categories, categories));
            list.setTextFilterEnabled(true);

            Cursor cursor = data.rawQuery("SELECT term, desc FROM words WHERE cat = '" + categories[position] + "'", null);
            startManagingCursor(cursor);

            String columns[] = new String[] { "term", "desc" };
            int[] to = new int[] { R.id.cat_term, R.id.cat_desc };

            SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.cat_result, cursor, columns, to);
            this.setListAdapter(myAdapter);


        }
        */
    }
}
